package com.jinandi.app.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jinandi.app.util.Image;

@Controller
@RequestMapping({ "/login/validateCode" })
public class ValidateCodeController {

	@RequestMapping({ "/image" })
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setDateHeader("Expires", 0L);

		response.setContentType("image/png");
		Image img = new Image();
		ImageIO.write(img.creatImage(), "PNG", response.getOutputStream());
	}

	@RequestMapping({ "/images" })
	@ResponseBody
	public void validateCode(HttpServletResponse response, HttpSession session) {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setDateHeader("Expires", 0L);

		response.setContentType("image/png");
		Image img = new Image();
		try {
			ImageIO.write(img.creatImage(), "PNG", response.getOutputStream());
			session.setAttribute("valcode", "cccc");//img.sRand 修改了
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping({ "/initLoad" })
	public String initLoad(Model model) {
		return "/validatecode/validate.ftl";
	}

	@RequestMapping({ "/loadcode" })
	@ResponseBody
	public String loadValidateCode(HttpServletRequest request) {
		String code1 = request.getSession().getAttribute("valcode").toString();

		return code1 + "=========";
	}

	@RequestMapping({ "/loadcode1" })
	@ResponseBody
	public String loadValidateCode1(HttpServletRequest request,
			HttpSession session, @ModelAttribute("valcode") String code) {
		String code1 = request.getSession().getAttribute("valcode").toString();
		String code2 = session.getAttribute("valcode").toString();

		return code1 + "=========" + "----------" + code2 + "===========----"
				+ code;
	}

	@ResponseBody
	@RequestMapping({ "/imagesvalid" })
	public byte[] validateCodeNew(HttpServletResponse response,
			HttpSession session) {
		Image img = new Image();
		BufferedImage buffer = img.creatImage();
		session.setAttribute("valcode", img.sRand);

		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setDateHeader("Expires", 0L);

		response.setContentType("image/png");
		ByteArrayOutputStream bao = new ByteArrayOutputStream();
		try {
			ImageIO.write(buffer, "PNG", bao);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bao.toByteArray();
	}
}